package demo03;

import java.util.HashSet;

public class Singleton {
    private Singleton() {
    }

    public static Singleton getInstance() {
        return SingletonHolder.instance;
    }

    public static void main(String[] args) {
        System.out.println(Singleton.getInstance());
        System.out.println(Singleton.getInstance());
        System.out.println(Singleton.getInstance());
        System.out.println(Singleton.getInstance());
        System.out.println(Singleton.getInstance());
        // 创建一个新的HashSet
        HashSet<String> set = new HashSet<>();

// 添加元素
        set.add("Hanser");
        set.add("Hanserwei");
        set.add("LikeYY");

// 输出HashSet的元素个数
        System.out.println("HashSet size: " + set.size()); // output: 3

// 判断元素是否存在于HashSet中
        boolean containsWanger = set.contains("Hanserwei");
        System.out.println("Does set contain 'Hanserwei'? " + containsWanger); // output: true

// 删除元素
        boolean removeWanger = set.remove("Hanserwei");
        System.out.println("Removed 'Hanserwei'? " + removeWanger); // output: true

// 修改元素，需要先删除后添加
        boolean removeChenmo = set.remove("Hanser");
        boolean addBuChenmo = set.add("hanser");
        System.out.println("Modified set? " + (removeChenmo && addBuChenmo)); // output: true

// 输出修改后的HashSet
        System.out.println("HashSet after modification: " + set); // output: [陈清扬, 不沉默]
    }

    private static class SingletonHolder {
        public static final Singleton instance = new Singleton();
    }
}